package de.dopichaj.labrador.tools;

import java.io.File;

/**
 * Information about a context (that is, information about a parent and its
 * children).
 */
public final class ContextInformation {
 
    private final String signature;
    private final int parentLength;
    private final String xPath;
    private final File file;

    /**
     * 
     * @param signature
     *            the signature; see {@link #getSignature()} for a description
     *            of the format.
     * @param parentLength the base 10 logarithm of the parent's lengh.
     * @param xPath
     */
    public ContextInformation(String signature, int parentLength, File file,
        String xPath) {
        
        this.signature = signature;
        this.parentLength = parentLength;
        this.file = file;
        this.xPath = xPath;
    }

    /**
     * Get the signature of this context. This signature consists of a sequence
     * of character pairs. The first character in a pair denotes the type of the
     * child ('e' for element or 't' for text), and the second character denotes
     * the base 10 logarithm of the number of tokens of this child (0 if the
     * child contains no words, 9 if the logarithm would be greater than 9).
     */
    public String getSignature() {
        return signature;
    }

    public String getXPath() {
        return xPath;
    }

    /**
     * Get a compact form of the XPath (only the last two components).
     */
    public String getCompactXPath() {
        return getXPath().replaceAll(".*(/[^/]*/[^/]*)", "/$1");
    }

    /**
     * Get the base 10 logarithm of the parent's length.
     */
    public int getParentLength() {
        return parentLength;
    }

    public File getFile() {
        return file;
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/